package beans.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageDemo
{

	public static void main(String[] args)
	{
		try
		{
			createRezCode(51, 20);
		}
		catch (ImageFormatException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 图形处理
	 * 
	 * @return
	 */
	public static String imRez()
	{
		BufferedImage image, image_biao;
		Graphics2D graphics;
		try
		{
			image = ImageIO.read(new FileInputStream("c:\\base.jpg"));
			image_biao = ImageIO.read(new FileInputStream("c:\\logo.gif"));

			graphics = image.createGraphics();
			graphics.drawImage(image_biao, 10, 10, image_biao.getWidth(null), image_biao.getHeight(null), null);
			graphics.dispose();

			FileOutputStream out = new FileOutputStream("c:\\out.jpg");

			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
			param.setQuality(1f, false);
			encoder.setJPEGEncodeParam(param);
			encoder.encode(image);
			out.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		String imString = "";
		return imString;
	}

	public static void createRezCode(int width, int height) throws ImageFormatException, IOException
	{
		FileOutputStream out = new FileOutputStream("d:/demo.png");// demo.img

		// 准备缓冲图像,不支持表单
		BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
		Random r = new Random();
		// 获取图形上下文环境
		Graphics gc = bimg.getGraphics();
		// 设定背景色并进行填充
		gc.setColor(getRandColor(200, 250));
		gc.fillRect(0, 0, width, height);
		// 设置图形上下文环境字体
		gc.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		// 随机产生200条干扰线条，使图像中的认证码不易被其他分析程序探测到
		gc.setColor(getRandColor(160, 200));

		for (int i = 0; i < 200; i++)
		{
			int x1 = r.nextInt(width);
			int y1 = r.nextInt(height);
			int x2 = r.nextInt(15);
			int y2 = r.nextInt(15);
			gc.drawLine(x1, y1, x1 + x2, y1 + y2);
		}
		// 随机产生100个干扰点，使图像中的验证码不易被其他分析程序探测到
		gc.setColor(getRandColor(120, 240));
		for (int i = 0; i < 100; i++)
		{
			int x = r.nextInt(width);
			int y = r.nextInt(height);
			gc.drawOval(x, y, 0, 0);
		}

		// 随机产生4个数字的验证码
		// String rs = "";
		String rn = "";
		for (int i = 0; i < 4; i++)
		{
			rn = String.valueOf(r.nextInt(10));
			// rs += rn;
			gc.setColor(new Color(20 + r.nextInt(110), 20 + r.nextInt(110), 20 + r.nextInt(110)));
			gc.drawString(rn, 13 * i + 1, 16);
		}
		// 释放图形上下文环境
		gc.dispose();

		JPEGImageEncoder codee = JPEGCodec.createJPEGEncoder(out);
		codee.encode(bimg);
		out.flush();
		out.close();
	}

	public static Color getRandColor(int fc, int bc)
	{
		Random r = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int red = fc + r.nextInt(bc - fc);// 红
		int green = fc + r.nextInt(bc - fc);// 绿
		int blue = fc + r.nextInt(bc - fc);// 蓝
		return new Color(red, green, blue);
	}

}
